from ddt import ddt, data, unpack
from unittest import TestCase
from selenium import webdriver
from SignData import SignData
from SignOperation import SignOperation
from DBUtils import DBUtils


@ddt
class TestSign(TestCase):
    @data(*SignData.sign_success_data)
    @unpack
    def testSign1(self, casename, username, password, repwd, expect):
        #  开始测试
        driver = webdriver.Chrome()
        # 打开浏览器页面并最大化
        driver.get("http://localhost:90")
        driver.maximize_window()

        dbutils = DBUtils()
        dbresult = dbutils.select('select username from user')
        num = len(dbresult)
        # 执行注册
        sign = SignOperation()
        sign.sign(driver, username, str(int(password)), str(int(repwd)))
        # 获取注册成功实际结果
        result = sign.get_success_result(driver, username, num)
        if result != expect:
            driver.save_screenshot(casename + '.jpg')
        else:
            print('通过')

        driver.quit()

        self.assertEqual(result, expect)

    @data(*SignData.sign_repeat_data)
    @unpack
    def testSign2(self, casename, username, password, repwd, expect):
        #  开始测试
        driver = webdriver.Chrome()
        # 打开浏览器页面并最大化
        driver.get("http://localhost:90")
        driver.maximize_window()

        dbutils = DBUtils()
        dbresult = dbutils.select('select username from user')
        num = len(dbresult)
        # 执行注册
        sign = SignOperation()
        sign.sign(driver, username, str(int(password)), str(int(repwd)))
        # 获取重复注册的实际结果
        result = sign.get_repeat_result(username, num)
        if result != expect:
            driver.save_screenshot(casename + '.jpg')
        else:
            print('通过')

        driver.quit()

        self.assertEqual(result, expect)

    @data(*SignData.sign_dipa_data)
    @unpack
    def testSign3(self, casename, username, password, repwd, expect):
        #  开始测试
        driver = webdriver.Chrome()
        # 打开浏览器页面并最大化
        driver.get("http://localhost:90")
        driver.maximize_window()

        dbutils = DBUtils()
        dbresult = dbutils.select('select username from user')
        num = len(dbresult)
        # 执行注册
        sign = SignOperation()
        sign.sign(driver, username, str(int(password)), str(int(repwd)))
        # 获取两次密码不同的实际结果
        result = sign.get_dipa_result(username, num)
        if result != expect:
            driver.save_screenshot(casename + '.jpg')
        else:
            print('通过')

        driver.quit()

        self.assertEqual(result, expect)

    @data(*SignData.sign_miss_data)
    @unpack
    def testSign4(self, casename, username, password, repwd, expect):
        #  开始测试
        driver = webdriver.Chrome()
        # 打开浏览器页面并最大化
        driver.get("http://localhost:90")
        driver.maximize_window()

        dbutils = DBUtils()
        dbresult = dbutils.select('select username from user')
        num = len(dbresult)
        # 执行注册
        sign = SignOperation()
        sign.sign(driver, username, str(int(password)), '')
        # 获取确实确认密码的实际结果
        result = sign.get_miss_result(driver, username, num)
        if result != expect:
            driver.save_screenshot(casename + '.jpg')
        else:
            print('通过')

        driver.quit()

        self.assertEqual(result, expect)
